home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Tools & Apps / Testing & Debugging / TV-Man Package / With Source Version / Source / TV-Man.Utility.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-13  |  11.5 KB  |  403 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    TV-Man.Utility.c
  4. #
  5. #    Copyright © Apple Computer, Inc. 1989-1990
  6. #    All rights reserved.
  7. #
  8. #    
  9. #
  10. #    This is a collection of functions that are not specificaly dedicated to
  11. #    any one part of the application but can be used across the board. See the following
  12. #    paragraphs for more definition of what can be included in this file.
  13. #
  14. #    In order to have an evironment which predictable events happen several definitions
  15. #    must be established. The most critical is in the file architecture. Each functional
  16. #    block will have its own source and header file. The main project file,  in this
  17. #    case TV-Man, will have its header file included with all other files. This will
  18. #    allow for global constants. The utility source file shall contain functions that
  19. #    are general purpose in nature and that can be used by all other functions. These
  20. #    are intended not to be application or major block specific. In order for this to be 
  21. #    accomodated all functions in the utility source file must use only the information
  22. #    that is passed to them or information that can be gleaned from the system via
  23. #    toolbox calls. There will be no header file associated with the utility file as this
  24. #    will destroy the intent of the utilities.
  25. #
  26. #    There are several files which contain information which is global in nature .These
  27. #    file are included in the main project header file. They are: x.Errors.h, x.Ext.h,
  28. #    x.Protos.h, x.Menus.h. The reason for containing them in seperate files is one of
  29. #    convienience and accesability. 
  30. #
  31. #
  32. #    Revision Log:
  33. #    
  34. #        9-4-91        RGK        Creation
  35. #
  36. #
  37. ------------------------------------------------------------------------------*/
  38.  
  39. /*    This include is here for the inclusion of the standard toolbox and system
  40.     definitions. This is not to be confused with the other definitions which
  41.     become avaliable with this file. That would null and void the standard disclamer
  42.     in the header statements    */
  43.     
  44. #include "TV-Man.h"
  45.  
  46.  
  47. /*---------------------------------------------------------------------------------------*/
  48. /*    This is called when an update event is received for a window.
  49.     It calls DrawWindow to draw the contents of an application window.
  50.     As an effeciency measure that does not have to be followed, it
  51.     calls the drawing routine only if the visRgn is non-empty. This
  52.     will handle situations where calculations for drawing or drawing
  53.     itself is very time-consuming. */
  54.  
  55. void DoUpdate(window)
  56. WindowPtr    window;
  57. {
  58.     if ( IsAppWindow(window) )
  59.     {
  60.         BeginUpdate(window);
  61.         if ( ! EmptyRgn(window->visRgn) )    /* draw if updating needs to be done */
  62.             DrawWindow(window);
  63.         EndUpdate(window);
  64.     }
  65. } /* DoUpdate */
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /*---------------------------------------------------------------------------------------*/
  73. /*    This is called when a window is activated or deactivated.
  74.     In TV-Man, the Window Manager's handling of activate and
  75.     deactivate events is sufficient */
  76.  
  77. void DoActivate(window, becomingActive)
  78. WindowPtr    window;
  79. Boolean        becomingActive;
  80. {
  81.     if ( IsAppWindow(window) )
  82.     {
  83.         if ( becomingActive )
  84.             /* do whatever you need to at activation */ ;
  85.         else
  86.             /* do whatever you need to at deactivation */ ;
  87.     }
  88. } /* DoActivate */
  89.  
  90.  
  91.  
  92.  
  93.  
  94. /*---------------------------------------------------------------------------------------*/
  95. /*    Get the global coordinates of the mouse. When you call OSEventAvail
  96.     it will return either a pending event or a null event. In either case,
  97.     the where field of the event record will contain the current position
  98.     of the mouse in global coordinates and the modifiers field will reflect
  99.     the current state of the modifiers. Another way to get the global
  100.     coordinates is to call GetMouse and LocalToGlobal, but that requires
  101.     being sure that thePort is set to a valid port. */
  102.  
  103. void GetGlobalMouse(mouse)
  104. Point    *mouse;
  105. {
  106.     EventRecord    event;
  107.     
  108.     OSEventAvail(kNoEvents, &event);    /* we aren't interested in any events */
  109.     *mouse = event.where;                /* just the mouse position */
  110. } /* GetGlobalMouse */
  111.  
  112.  
  113.  
  114.  
  115.  
  116. /*---------------------------------------------------------------------------------------*/
  117. /*    Check to see if a window belongs to the application. If the window pointer
  118.     passed was NIL, then it could not be an application window. WindowKinds
  119.     that are negative belong to the system and windowKinds less than userKind
  120.     are reserved by Apple except for windowKinds equal to dialogKind, which
  121.     mean it is a dialog. In order to reduce the chance of accidentally treating
  122.     some window as an AppWindow that shouldn't be, we'll only return true if the
  123.     windowkind is userKind. If you add different kinds of windows to TV-Man
  124.     you'll need    to change how this all works. */
  125.  
  126. Boolean IsAppWindow(window)
  127. WindowPtr    window;
  128. {
  129.     short        windowKind;
  130.  
  131.     if ( window == nil )
  132.         return false;
  133.     else
  134.     {                                /* application windows have windowKinds = userKind (8) */
  135.         windowKind = ((WindowPeek) window)->windowKind;
  136.         return (windowKind == userKind);
  137.     }
  138. } /* IsAppWindow */
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. /*---------------------------------------------------------------------------------------*/
  146. /*    Check to see if a window belongs to a desk accessory. */
  147.  
  148. Boolean IsDAWindow(window)
  149. WindowPtr    window;
  150. {
  151.     if ( window == nil )
  152.         return false;
  153.     else                            /* DA windows have negative windowKinds */
  154.         return ((WindowPeek) window)->windowKind < 0;
  155. } /* IsDAWindow */
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. /*---------------------------------------------------------------------------------------*/
  163. /*    Display an alert that tells the user an error occurred. If the error type is "eMajor"
  164.     then exit the program. This is used as an ultimate bail-out for serious errors that
  165.     prohibit the continuation of the application. The error number is used to index an
  166.     'STR#' resource so that a relevant message can be displayed. */
  167.  
  168. void Error(type, error)
  169. short    type, error;
  170. {
  171.     short        itemHit;
  172.     Str255        message;
  173.  
  174.     SetCursor(&qd.arrow);
  175.     GetIndString(message, type, error);
  176.     ParamText(message, "", "", "");
  177.     itemHit = Alert(type, nil);
  178.     if(type == eMajor) ExitToShell();
  179. } /* Error */
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. /*---------------------------------------------------------------------------------------*/
  188. /*    Close a window. This handles desk accessory and application windows. If there was
  189.      a document associated with a window, you could do any document saving processing
  190.     if it is 'dirty'.DoCloseWindow would return true if the window actually closed, i.e.,
  191.     the user didn’t cancel from a save dialog. This result is handy when
  192.     the user quits an application, but then cancels the save of a document
  193.     associated with a window. */
  194.  
  195.  
  196.  
  197. Boolean DoCloseWindow(window)
  198. WindowPtr    window;
  199. {
  200.     if ( IsDAWindow(window) )
  201.         CloseDeskAcc(((WindowPeek) window)->windowKind);
  202.     else if ( IsAppWindow(window) )
  203.         CloseWindow(window);
  204.     return true;
  205. } /* DoCloseWindow */
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213. /*---------------------------------------------------------------------------------------*/
  214. /*    Clean up the application and exit. We close all of the windows so that
  215.      they can update their documents, if any. If we find out that a cancel has
  216.      occurred, we won't exit to the shell, but will return instead. */
  217.  
  218. void Terminate()
  219. {
  220.     WindowPtr    aWindow;
  221.     Boolean        closed;
  222.     
  223.     closed = true;
  224.     do
  225.     {
  226.         aWindow = FrontWindow();                /* get the current front window */
  227.         if (aWindow != nil)
  228.             closed = DoCloseWindow(aWindow);    /* close this window */    
  229.     }
  230.     while (closed && (aWindow != nil));
  231.     if (closed)
  232.         ExitToShell();                            /* exit if no cancellation */
  233. } /* Terminate */
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240. /*----------------------------------------------------------------------------------*/
  241. /*    This function checks the value of the edit box against the values passed. If the
  242.     value in the edit box is over the limits passed the edit box will be modified
  243.     to reflect these limits.    */
  244.  
  245. void RangeCheckShort(dialog, itemid, minimum, maximum)
  246. DialogPtr    dialog;
  247. short        itemid, minimum, maximum;
  248. {
  249.     short        textvalue, value;
  250.  
  251.     textvalue = Text2Short(dialog, itemid);
  252.     value = CheckValueRange(textvalue, minimum, maximum);
  253.     Short2Text(value, dialog, itemid);
  254. }/* RangeCheckShort */
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261. /*----------------------------------------------------------------------------------*/
  262. /*    this function sets the specified text box id with the value passed    */
  263.  
  264. void Short2Text(value, dialog, itemid)
  265. short        value, itemid;
  266. DialogPtr    dialog;
  267. {
  268.     short        itemtype;
  269.     Handle        itemhdl;
  270.     Rect        itemrect;
  271.     Str255        string;
  272.  
  273.     NumToString((long)value, &string);
  274.     GetDItem(dialog, itemid, &itemtype, &itemhdl, &itemrect);
  275.     SetIText(itemhdl, string);
  276. }/* SetTheText */
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283. /*----------------------------------------------------------------------------------*/
  284. /*    This function gets the specified text box id with the value passed. Since the
  285.     GetIText returns a long and we need to return a short a test is made on the value
  286.     to determine how to truncate it. */
  287.  
  288. short Text2Short(dialog, itemid)
  289. short        itemid;
  290. DialogPtr    dialog;
  291. {
  292. short        itemtype;
  293. Handle        itemhdl;
  294. Rect        itemrect;
  295. long        longvalue;
  296. Str255        string;
  297.  
  298.     GetDItem(dialog, itemid, &itemtype, &itemhdl, &itemrect);
  299.     GetIText(itemhdl, &string);
  300.     StringToNum(string, &longvalue);
  301.     if(longvalue > 32767) return(32767);
  302.     if(longvalue < -32768) return(-32768);
  303.     return((short)LoWrd(longvalue));
  304. }/* GetTheText */
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311. /*----------------------------------------------------------------------------------*/
  312. /*    This function toggles the control value from the Min control value to the Max
  313.     control value. The control handle is passed to identify which control is to be 
  314.     toggled.    */
  315.  
  316. void ToggleControl(control)
  317. ControlHandle    control;
  318. {
  319.     if(GetCtlValue(control) == GetCtlMax(control))
  320.     {
  321.         SetCtlValue(control, GetCtlMin(control));
  322.     }
  323.     else
  324.     {
  325.         SetCtlValue(control, GetCtlMax(control));
  326.     }
  327. }/* ToggleControl */
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. /*----------------------------------------------------------------------------------*/
  335. /*    This function returns a value which divides the range of the scroll area of the
  336.     control passed with the amount passed. The actual number may be off because of
  337.     roundoff errors. Because of this the calling function should make sure the control
  338.     value dosn't extend beyond the controls limits.    */
  339.     
  340. short JumpAmount(control, amount)
  341. ControlHandle    control;
  342. short            amount;
  343. {
  344.     short        value;
  345.     
  346.     value = GetCtlMax(control) - GetCtlMin(control);
  347.     value = value / amount;
  348.     return(value);
  349. }/* JumpAmount */
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358. /*----------------------------------------------------------------------------------*/
  359. /*    This function tests the passed value to see if it is in the range specified by
  360.     the Minimum and Maximum levels in the control identified by the passed handle. If
  361.     the value is in the range the value will be returned unchanged. If either limit
  362.     is exceeded the returned value will be closest limit value.    */
  363.  
  364. short CheckControlRange(value, control)
  365. short            value;
  366. ControlHandle    control;
  367. {
  368.     if(value > GetCtlMax(control)) return(GetCtlMax(control));
  369.     if(value < GetCtlMin(control)) return(GetCtlMin(control));
  370.     return(value);
  371. }/* CheckControlRange */
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379. /*----------------------------------------------------------------------------------*/
  380. /*    This function tests the passed value to see if it is in the range specified by
  381.     the levels which are passed as well. If the value is in the range the value
  382.     will be returned unchanged. If either limit is exceeded the returned value will
  383.     be closest limit value. Since these are short integers there is a further test
  384.     which will divide up the negative area to test for values which are over 32K
  385.     but should not be interpreted as negative. This last part is sort of a hack    */
  386.  
  387. short CheckValueRange(value, minimum, maximum)
  388. short            value, minimum, maximum;
  389. {
  390.     if(value > maximum) return(maximum);
  391.     if(value < minimum)
  392.     {
  393.         if(value < -16384)
  394.         {
  395.             return(maximum);
  396.         }
  397.         else
  398.         {
  399.             return(minimum);
  400.         }
  401.     }
  402.     return(value);
  403. }/* CheckValueRange */